package com.example.chamberlainserver.Service.impl;

import com.example.chamberlainserver.Entry.ScheduleRule;
import com.example.chamberlainserver.Entry.User;
import com.example.chamberlainserver.Service.ScheduleRuleService;
import com.example.chamberlainserver.Service.UserService;
import com.example.chamberlainserver.dto.PageResult;
import com.example.chamberlainserver.mapper.ScheduleRuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 排班规则服务实现类
 */
@Service
public class ScheduleRuleServiceImpl implements ScheduleRuleService {

    @Autowired
    private ScheduleRuleMapper scheduleRuleMapper;
    
    @Autowired
    private UserService userService;

    @Override
    public boolean addScheduleRule(ScheduleRule scheduleRule) {
        scheduleRule.setId(UUID.randomUUID().toString());
        scheduleRule.setCreatedAt(LocalDateTime.now());
        scheduleRule.setUpdatedAt(LocalDateTime.now());
        if (scheduleRule.getIsActive() == null) {
            scheduleRule.setIsActive(true);
        }
        if (scheduleRule.getPriority() == null) {
            scheduleRule.setPriority(1);
        }
        return scheduleRuleMapper.insertScheduleRule(scheduleRule) > 0;
    }

    @Override
    public ScheduleRule getScheduleRuleById(String id) {
        ScheduleRule scheduleRule = scheduleRuleMapper.getScheduleRuleById(id);
        if (scheduleRule != null && scheduleRule.getCreatedBy() != null) {
            User creator = userService.getUserById(scheduleRule.getCreatedBy());
            scheduleRule.setCreator(creator);
        }
        return scheduleRule;
    }

    @Override
    public List<ScheduleRule> getScheduleRulesByName(String name) {
        List<ScheduleRule> scheduleRules = scheduleRuleMapper.getScheduleRulesByName(name);
        
        for (ScheduleRule scheduleRule : scheduleRules) {
            if (scheduleRule.getCreatedBy() != null) {
                User creator = userService.getUserById(scheduleRule.getCreatedBy());
                scheduleRule.setCreator(creator);
            }
        }
        
        return scheduleRules;
    }

    @Override
    public List<ScheduleRule> getActiveScheduleRules() {
        List<ScheduleRule> scheduleRules = scheduleRuleMapper.getActiveScheduleRules();
        
        for (ScheduleRule scheduleRule : scheduleRules) {
            if (scheduleRule.getCreatedBy() != null) {
                User creator = userService.getUserById(scheduleRule.getCreatedBy());
                scheduleRule.setCreator(creator);
            }
        }
        
        return scheduleRules;
    }

    @Override
    public List<ScheduleRule> getAllScheduleRules() {
        List<ScheduleRule> scheduleRules = scheduleRuleMapper.getAllScheduleRules();
        
        for (ScheduleRule scheduleRule : scheduleRules) {
            if (scheduleRule.getCreatedBy() != null) {
                User creator = userService.getUserById(scheduleRule.getCreatedBy());
                scheduleRule.setCreator(creator);
            }
        }
        
        return scheduleRules;
    }

    @Override
    public boolean updateScheduleRule(ScheduleRule scheduleRule) {
        scheduleRule.setUpdatedAt(LocalDateTime.now());
        return scheduleRuleMapper.updateScheduleRule(scheduleRule) > 0;
    }

    @Override
    public boolean deleteScheduleRule(String id) {
        return scheduleRuleMapper.deleteScheduleRuleById(id) > 0;
    }

    @Override
    public boolean updateScheduleRuleStatus(String id, Boolean isActive) {
        return scheduleRuleMapper.updateScheduleRuleStatus(id, isActive) > 0;
    }

    @Override
    public PageResult<ScheduleRule> getPagedScheduleRuleList(Map<String, Object> params) {
        // 查询总记录数
        Long total = scheduleRuleMapper.getPagedScheduleRuleCount(params);
        
        // 查询数据列表
        List<ScheduleRule> scheduleRules = scheduleRuleMapper.getPagedScheduleRuleList(params);
        
        // 为每个记录加载创建人信息
        for (ScheduleRule scheduleRule : scheduleRules) {
            if (scheduleRule.getCreatedBy() != null) {
                User creator = userService.getUserById(scheduleRule.getCreatedBy());
                scheduleRule.setCreator(creator);
            }
        }
        
        Integer current = (Integer) params.getOrDefault("current", 1);
        Integer pageSize = (Integer) params.getOrDefault("pageSize", 10);
        
        return new PageResult<>(current, pageSize, total, scheduleRules);
    }

    @Override
    public boolean validateScheduleRules(List<ScheduleRule> rules) {
        if (rules == null || rules.isEmpty()) {
            return false;
        }
        
        // 验证规则的基本信息
        for (ScheduleRule rule : rules) {
            if (rule.getName() == null || rule.getName().trim().isEmpty()) {
                return false;
            }
            if (rule.getMinStaffPerSlot() == null || rule.getMinStaffPerSlot() <= 0) {
                return false;
            }
            if (rule.getMaxStaffPerSlot() == null || rule.getMaxStaffPerSlot() <= 0) {
                return false;
            }
            if (rule.getMinStaffPerSlot() > rule.getMaxStaffPerSlot()) {
                return false;
            }
            if (rule.getMaxHoursPerWeek() == null || rule.getMaxHoursPerWeek() <= 0) {
                return false;
            }
            if (rule.getMinRestHours() == null || rule.getMinRestHours() <= 0) {
                return false;
            }
            
            // 验证约束条件
            if (rule.getConstraints() != null) {
                ScheduleRule.Constraints constraints = rule.getConstraints();
                if (constraints.getMaxConsecutiveDays() != null && 
                    (constraints.getMaxConsecutiveDays() <= 0 || constraints.getMaxConsecutiveDays() > 7)) {
                    return false;
                }
                if (constraints.getMaxShiftsPerWeek() != null && 
                    (constraints.getMaxShiftsPerWeek() <= 0 || constraints.getMaxShiftsPerWeek() > 7)) {
                    return false;
                }
            }
        }
        
        return true;
    }
}